package com.jstyle.jclife.utils;

import android.app.Notification;
import android.content.Context;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;

import com.jstyle.blesdk.Util.SingleDealData;
import com.jstyle.blesdk.cmdenum.SendCmdState;
import com.jstyle.blesdk.model.Notifier;
import com.jstyle.jclife.R;
import com.jstyle.jclife.app.MyApplication;
import com.jstyle.jclife.ble.BleManager;
import com.jstyle.jclife.daoManager.NotifyDataDaoManager;
import com.jstyle.jclife.model.JstyleDevice;
import com.jstyle.jclife.model.NotifyData;


public class NotificationListener extends NotificationListenerService {
    private long time;
    private String spName = "jstyle_userinfo";
    private String DEVICE_TYPE = "device_type";
    private static final String TAG = "NotificationListener";
    private static final String PackageName_WeChat = "com.tencent.mm";
    private static final String PackageName_Facebook = "com.facebook.katana";
    private static final String PackageName_Facebook_orca = "com.facebook.orca";
    private static final String PackageName_Twitter = "com.twitter.android";
    private static final String PackageName_Skype = "com.skype.raider";
    private static final String PackageName_WhatsApp = "com.whatsapp";
    private String[]notifyArray;
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // TODO Auto-generated method stub

        String packageName = sbn.getPackageName();
        Notification n = sbn.getNotification();
        String info = "";
        Context context=getApplicationContext();
        notifyArray=context.getResources().getStringArray(R.array.notify_array_listener);

        Log.i(TAG, "onNotificationPosted: " + packageName);
        if (System.currentTimeMillis() - time < 1000)
            return;
        Notifier notifier = new Notifier();
        JstyleDevice jstyleDevice= MyApplication.getJstyleDevice();
        if(jstyleDevice!=null)notifier.setNotifierType(jstyleDevice.getNotifierType());
        String titleNotify="";
        int typeNotifier = -1;
        int  notifyId = -1;
        switch (packageName) {
            case PackageName_Facebook:
                info=String.valueOf(n.tickerText);
                typeNotifier = Notifier.Data_Facebook;
                notifyId=3;
                break;
            case PackageName_Facebook_orca:
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    Bundle bundle=n.extras;
                    String title=bundle.getString(Notification.EXTRA_TITLE);
                    CharSequence[]array=bundle.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
                    String bigText=bundle.getString(Notification.EXTRA_BIG_TEXT);
                    String content="";
                    if(array!=null) {
                        for(int i=0;i<array.length;i++){
                            content+=array[i].toString();
                        }
                    }
                    if(!TextUtils.isEmpty(bigText)){
                        content+=bigText;
                    }
                    info=title+":"+content;
                }
                typeNotifier = Notifier.Data_Facebook;
                notifyId=3;
                break;
            case PackageName_Twitter:
                info=String.valueOf(n.tickerText);
                typeNotifier = Notifier.Data_Twitter;
                notifyId=4;
                break;
            case PackageName_WeChat:
                typeNotifier = Notifier.Data_WeChat;
                info = String.valueOf(n.tickerText);
                int lastIndex=info.lastIndexOf(":");
                if(lastIndex!=-1){
                    titleNotify=info.substring(0,lastIndex);
                    info=info.substring(lastIndex+1).trim();
                    Log.i(TAG, "onNotificationPosted: "+titleNotify);
                }
                Log.i(TAG, "onNotificationPosted: "+info);
                notifyId=2;
                break;
            case PackageName_Skype:
                typeNotifier = Notifier.Data_Skype;
                info = String.valueOf(n.tickerText);
                notifyId=5;
                break;
            case PackageName_WhatsApp:
                typeNotifier = Notifier.Data_WhatApp;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    Bundle bundle=n.extras;
                    String title=bundle.getString(Notification.EXTRA_TITLE);
                    CharSequence[]array=bundle.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
                    String bigText=bundle.getString(Notification.EXTRA_BIG_TEXT);
                    String content="";
                    if(array!=null) {
                        content=array[array.length-1].toString();
                    }
                    if(!TextUtils.isEmpty(bigText)){
                        content+=bigText;
                    }
                    info=title+content;
                }
                notifyId=6;
                break;
        }
        if (typeNotifier == -1) return;
        notifier.setType(typeNotifier);
        notifier.setInfo(info);
        notifier.setTitle(titleNotify);
        sendNotify(notifier, notifyId);
    }

    private void sendNotify(Notifier notifier,long id) {

        final NotifyData notifyData = getNotifyData(id);
        if (!notifyData.isEnable()) return;
        BleManager.getInstance().writeValue(
                SingleDealData.sendData(SendCmdState.SET_NOTIFIER, notifier));
    }

    private NotifyData getNotifyData(long id) {
        NotifyData notifyData = NotifyDataDaoManager.getNotifyData(id);
        return notifyData;
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // TODO Auto-generated method stub

    }

}
